<?php
declare (strict_types = 1);

namespace app\controller\api\v1;

use think\Request;
use think\Validate;
use app\model\User;
use app\event\UserLoginEvent;

class UserController
{
    // 会员 - 注册
    public function register(Request $request)
    {
        // users.username
        $username = $request->post('username', '');
        // users.password
        $password = $request->post('password', '');

        $check_data = [
            'username'  => $username,
            'password'  => $password,
        ];
        $rules = [
            'username|用户名'  => 'require|length:3,20|alphaNum',
            'password|密码'    => 'require|length:6,12|alphaNum'
        ];
        $message = [
            'username.require'  => ':attribute 不能为空',
            'username.length'   => ':attribute 长度在 3~20 之间',
            'username.alphaNum' => ':attribute 必须是字母和数字',
            'password.require'  => ':attribute 不能为空',
            'password.length'   => ':attribute 长度在 3~20 之间',
            'password.alphaNum' => ':attribute 必须是字母和数字',
        ];

        $validate = new Validate;
        $validate->message($message);
        if ($validate->check($check_data, $rules) === false)
        {
            $error = [
                'message'   => $validate->getError(),
                'data'      => null,
                'code'      => 200
            ];
            return json($error, 403);
        }

        if (User::where('username', $username)->count() > 0)
        {
            $error = [
                'code'      => 403,
                'message'   => 'username 已存在',
                'data'      => null
            ];
            return json($error, 403);
        }

        try {
            User::generateLog($username, $password);
        } catch (\Exception $e){
            $error = [
                'code'      => 403,
                'message'   => '注册失败，请重试',
                'data'      => null,
            ];
            return json($error, 403);
        }

        $data = [
            'code'      => 200,
            'message'   => 'success',
            'data'      => null,
        ];
        return json($data, 200);
    }// register() end

    // 会员-登录
    public function login(Request $request)
    {
        // users.username
        $username = $request->post('username', '');
        // users.password
        $password = $request->post('password', '');

        $check_data = [
            'username'  => $username,
            'password'  => $password,
        ];
        $rules = [
            'username|用户名'  => 'require|length:3,20|alphaNum',
            'password|密码'    => 'require|length:6,12|alphaNum'
        ];
        $message = [
            'username.require'  => ':attribute 不能为空',
            'username.length'   => ':attribute 长度在 3~20 之间',
            'username.alphaNum' => ':attribute 必须是字母和数字',
            'password.require'  => ':attribute 不能为空',
            'password.length'   => ':attribute 长度在 3~20 之间',
            'password.alphaNum' => ':attribute 必须是字母和数字',
        ];

        $validate = new Validate;
        $validate->message($message);
        if ($validate->check($check_data, $rules) === false)
        {
            $error = [
                'message'   => $validate->getError(),
                'data'      => null,
                'code'      => 200
            ];
            return json($error, 403);
        }

        if (User::where('username', $username)->count() < 1)
        {
            $error = [
                'code'      => 403,
                'message'   => 'username 不存在',
                'data'      => null,
            ];
            return json($error, 403);
        }

        $user     = User::where('username', $username)->findOrEmpty();
        $password = User::generatePassword($password, $user->salt);
        if (User::where('username', $username)->where('password', $password)->count() < 1)
        {
            $error = [
                'code'      => 403,
                'message'   => 'password 错误',
                'data'      => null,
            ];
            return json($error, 403);
        }

        event(new UserLoginEvent($user, $request));
        $data = [
            'code'      => 200,
            'message'   => 'success',
            'data'      => null,
        ];
        return json($data, 200);
    } // login() end
}
